﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using He.Common;

namespace He.Web.manage.contents
{
    public partial class list : He.Web.UI.Manage
    {
        public int channel_id;                         //栏目ID
        protected int pageSize = 12;                   //每页记录数
        protected int page;                            //当前页
        protected int pcount;                          //总页数
        public Model.channel channel;
        protected void Page_Load(object sender, EventArgs e)
        {
            page = GetInt("page", 0);

            if (!int.TryParse(Request.Params["channel_id"] as string, out this.channel_id))
            {
                ShowParamError();
                return;
            }

            channel = new He.BLL.channel().GetModelByCache(channel_id);

            if (!IsPostBack)
            {
                chkLoginLevel("viewChannel" + channel.id);

                RptBind();
            }
        }

        #region 数据绑定
        private void RptBind()
        {
            BLL.article bll = new BLL.article();
            string strWhere = CombSqlTxt(this.channel_id);
            rptList.DataSource = bll.GetPageList(pageSize, page, strWhere, channel.order_by);
            rptList.DataBind();
            pcount = bll.GetRecordCount(strWhere);
        }
        #endregion

        //删除
        protected void lbtnDel_Click(object sender, EventArgs e)
        {
            chkLoginLevel("delChannel" + channel.id);
            BLL.article bll = new BLL.article();
            for (int i = 0; i < rptList.Items.Count; i++)
            {
                CheckBox cb = (CheckBox)rptList.Items[i].FindControl("Check");
                int id = int.Parse(((Label)rptList.Items[i].FindControl("lb_id")).Text);
                Model.article model = bll.GetModel(id);
                if (cb.Checked)
                {
                    if (model.is_sys == 1)
                    {
                        JscriptMsg(350, 230, "操作失败", "<b>错误提示！</b>您要删除的数据受系统保护。", "back", "Error");
                        continue;
                    }
                    bll.Delete(id);
                }
            }
            SaveLogs(string.Format("[{0}管理]批量删除", channel.manage_title));
            JscriptPrint("批量删除成功啦！", Request.RawUrl, "Success");
        }

        //保存排序
        protected void lbtnSave_Click(object sender, EventArgs e)
        {
            chkLoginLevel("editChannel" + channel.id);
            BLL.article bll = new BLL.article();
            for (int i = 0; i < rptList.Items.Count; i++)
            {
                CheckBox cb = (CheckBox)rptList.Items[i].FindControl("Check");
                int id = int.Parse(((Label)rptList.Items[i].FindControl("lb_id")).Text);
                int sortId;
                if (!int.TryParse(((TextBox)rptList.Items[i].FindControl("txtSortId")).Text.Trim(), out sortId))
                {
                    sortId = 99;
                }
                bll.UpdateField(id, "sort_id=" + sortId.ToString());
            }
            SaveLogs(string.Format("[{0}管理]批量保存排序", channel.manage_title));
            JscriptPrint("排序保存成功啦！", Request.RawUrl, "Success");
        }

    }
}
